<template>
  <!-- 商品分类导航 -->
  <div class="type-nav">
    <div class="container">
      <div @mouseleave="leaveHandler()" @click="gosearch" @mouseenter="enter()">
        <h2 class="all">全部商品分类</h2>
        <!-- 动画过度效果，transition须结合v-show/v-if使用，写入样式.name-enter(.sort-enter)
              .sort-enter{height: 0;} .sort-enter-to{height: 472px;} .sort-enter-active{} -->
        <transition name="sort">
          <div class="sort" v-show="show">
            <div class="all-sort-list2">
              <!-- 一级 -->
              <div
                class="item"
                v-for="(c1, index) in categoryList"
                :key="c1.categoryId"
                :class="{ cur: currentIndex == index }"
              >
                <h3 @mouseenter="enterHandler(index)">
                  <a
                    :data-categoryName="c1.categoryName"
                    :data-category1ID="c1.categoryId"
                    >{{ c1.categoryName }}</a
                  >
                </h3>
                <!-- 二级、三级 -->
                <div
                  class="item-list clearfix"
                  :style="{ display: currentIndex == index ? 'block' : 'none' }"
                >
                  <div
                    class="subitem"
                    v-for="(c2, index) in c1.categoryChild"
                    :key="c2.categoryId"
                  >
                    <dl class="fore">
                      <dt>
                        <a
                          :data-categoryName="c2.categoryName"
                          :data-category2ID="c2.categoryId"
                          >{{ c2.categoryName }}</a
                        >
                      </dt>
                      <dd>
                        <em
                          v-for="(c3, index) in c2.categoryChild"
                          :key="c3.categoryId"
                        >
                          <a
                            :data-categoryName="c3.categoryName"
                            :data-category3ID="c3.categoryId"
                            >{{ c3.categoryName }}</a
                          >
                        </em>
                      </dd>
                    </dl>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </transition>
      </div>
      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";

//底下的这种写法:是将lodash全部API引入,将来项目打包的时候，体积会大一些
// import _ from "lodash";
//引入手段:按需引入
import throttle from "lodash/throttle";
import bus from "@/assets/bus"
export default {
  name: "TypeNav",
  data() {
    return {
      currentIndex: -1,
      show: true,
      pageNo: 1,
    };
  },
  mounted() {
    if (this.$route.path != "/home") {
      this.show = false;
    }
  },
  computed: {
    ...mapState({
      // categoryList右侧需要的是一个函数，state为大仓库中的数据
      categoryList: (state) => {
        return state.home.categoryList;
      },
    }),
  },
  methods: {
    // 一级导航分类动态背影
    enterHandler: throttle(function (index) {
      this.currentIndex = index;
      //鼠标进入事件,假如用户的行为过快,会导致项目业务丢失【里面业务有很多，可能出现卡顿现象】。
      //一句话：用户行为过快,浏览器反应不过来,导致业务丢失!!!!
      //函数的防抖与节流技术
      // console.log("鼠标进入" + index);
    }, 10),
    // 一级导航分类鼠标离开
    leaveHandler() {
      this.currentIndex = -1;
      // 不在home页面是隐藏，例如搜索页面
      if (this.$route.path != "/home") {
        this.show = false;
      }
    },
    // 进入一级分类显示
    enter() {
      this.show = true;
    },
    gosearch(event) {
      //第一个问题:div父节点子元素太多【h3、h2、em、dt、dd、dl...a】？你怎么知道你点击的一定是a
      //第二个问题:要区分一级分类、二级分类、三级分类的a标签【category1Id|category2Id|category2Id】
      let targetNode = event.target;
      // console.log("targetNode", targetNode)
      // e.target.dataset与e.currentTarget.dataset的作用：获取标签中定义的值，定义方法 data-*=某个值
      let { categoryname, category1id, category2id, category3id } =
        targetNode.dataset;
      if (categoryname) {
        let locations = {name: "search"}
        let query = { categoryName: categoryname };
        if (category1id) {
          query.category1ID = category1id;
        } else if (category2id) {
          query.category2ID = category2id;
        } else {
          query.category3ID = category3id;
        }
        //点击商品分类按钮的时候,如果路径当中携带params参数,需要合并携带给search模块
        if (this.$route.params) {
          locations.params = this.$route.params;
          locations.query = query;
          //目前商品分类这里携带参数只有query参数
          this.$router.push(locations);
        }
      }

      bus.$emit("changePageNO", this.pageNo)
      // this.$router.push("./search")
    },
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 472px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;

            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                  }
                }
              }
            }
          }

          // 用js控制一级与二级、三级联动，css注释掉
          // &:hover {
          //   .item-list {
          //     display: block;
          //   }
          // }
        }

        // 用js控制一级背景颜色，css注释掉
        // .item:hover{
        //   background: skyblue;
        // }

        .cur {
          background: skyblue;
        }
      }
    }

    .sort-enter {
      height: 0;
    }
    .sort-enter-to {
      height: 472px;
    }
    .sort-enter-active {
      transition: all 0.5s linear;
    }
  }
}
</style>